﻿

var zindexManager = function () {
    this.layouts = {};
    this.getTop = function (name) {
        if (this.exists(name)) {
            throw new Error("名称为" + name + "的层已存在");
        }
        var zindex = this._gettop();
        this.layouts[name] = { zindex: zindex + 1 }
        return zindex + 1;
    };
    this.remove = function (name) {
        if (this.layouts[name]) {
            delete this.layouts[name];
        }
    };
    this.exists = function (name) {
        if (name == null) return false;
        for (var layout in this.layouts) {
            if (layout == name) {
                return true;
            }
        }
        return false;
    };
    this.getindex = function (name) {
        if (name == null) return false;
        for (var layout in this.layouts) {
            if (layout == name) {
                return layout.zindex;
            }
        }
        return false;
    };
    this.moveTotop = function (name) {
        if (!this.exists(name)) {
            return this.getTop(name);
        }
        var currindex = this.getindex(name);
        var zindex = this._gettop();
        if (currindex == zindex) {
            return currindex;
        }
        this.layouts[name] = { zindex: zindex + 1 }
        return zindex + 1;
    };
    this._gettop = function () {
        var result = 100;
        for (var layout in this.layouts) {
            if (this.layouts[layout] && (this.layouts[layout].zindex > result)) {
                result = this.layouts[layout].zindex;
            }
        }
        return result;
    }
};


var application = function () {
    this.tabPanelContainerHelper = null;
    this.zindexManager = new zindexManager();
    this.dialogManager = new dialogManager(this);
    this.alertManager = new alertManager(this);
    this.ajaxManager = new ajaxManager(this);
    /*
    *   {url,name,postdata}
    */
    this.addRemotePanel = function (panel) {
        this.getTabPanelContainer().addTab(panel);
    };
    this.removeRemotePanel = function (panel) {
        this.getTabPanelContainer().removeTab(panel.getId());
    }
    this.setTabPanelContainer = function (container) {
        this.tabPanelContainerHelper = container;
    }
    this.getTabPanelContainer = function () {
        return this.tabPanelContainerHelper;
    };
    this.getZindexManager = function () {
        return this.zindexManager;
    };
    this.getDialogManager = function () {
        return this.dialogManager;
    };
    this.getAlertManager = function () {
        return this.alertManager;
    };
    this.getAjaxManager = function () {
        return this.ajaxManager;
    };
    this.layout = function () {
        $(".layoutable").trigger("layout");
    }   
}
var App = new application();
